package co.yixiang.hw.service.impl;

import co.yixiang.hw.domain.YxStoreCart;
import co.yixiang.hw.vo.YxStoreCartListVo;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.hw.repository.YxStoreCartRepository;
import co.yixiang.hw.service.YxStoreCartService;
import co.yixiang.hw.service.dto.YxStoreCartDto;
import co.yixiang.hw.service.dto.YxStoreCartQueryCriteria;
import co.yixiang.hw.service.mapper.YxStoreCartMapper;
import com.alibaba.fastjson.JSONArray;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author Walert
* @date 2020-02-13
*/
@Service
//@CacheConfig(cacheNames = "yxStoreCart")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreCartServiceImpl implements YxStoreCartService {

    @Resource
    private  YxStoreCartRepository yxStoreCartRepository;
    @Resource
    private  YxStoreCartMapper yxStoreCartMapper;


    @Override
    //@Cacheable
    public Map<String,Object> queryAll(YxStoreCartQueryCriteria criteria, Pageable pageable){
        Page<YxStoreCart> page = yxStoreCartRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxStoreCartMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<YxStoreCartDto> queryAll(YxStoreCartQueryCriteria criteria){
        return yxStoreCartMapper.toDto(yxStoreCartRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public YxStoreCartDto findById(Long id) {
        YxStoreCart yxStoreCart = yxStoreCartRepository.findById(id).orElseGet(YxStoreCart::new);
        ValidationUtil.isNull(yxStoreCart.getId(),"YxStoreCart","id",id);
        return yxStoreCartMapper.toDto(yxStoreCart);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public YxStoreCartDto create(YxStoreCart resources) {
        return yxStoreCartMapper.toDto(yxStoreCartRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(YxStoreCart resources) {
        YxStoreCart yxStoreCart = yxStoreCartRepository.findById(resources.getId()).orElseGet(YxStoreCart::new);
        ValidationUtil.isNull( yxStoreCart.getId(),"YxStoreCart","id",resources.getId());
        yxStoreCart.copy(resources);
        yxStoreCartRepository.save(yxStoreCart);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(JSONArray ids) {
        for (int i = 0; i < ids.size(); i++) {
            YxStoreCart yxStoreCart = new YxStoreCart();
            yxStoreCart.setId(ids.getLong(i));
            yxStoreCart.setIsDel(1);
            this.update(yxStoreCart);
        }
    }


    /**
     * 查询用户所有数据不分页
     *
     * @param uid 用户ID
     * @return List<YxStoreCartDto>
     */
    @Override
    public List<Map> queryUserAll(Long uid) {
        return yxStoreCartRepository.findUserCartList(uid);
    }

    /**
     * 异步删除购物车数据
     *
     * @param ids
     */
    @Override
    public void asyncDelCartInfo(JSONArray ids) {
        this.deleteAll(ids);
        System.out.println("删除购物车信息完成");
    }


}